package cn.max.controllers;

import cn.max.plat.utils.ReflectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *@author Val.(Valentine Vincent) E-mail:valer@126.com
 *@version 1.0
 *@date 2013-12-25
 *Instruction : 
 **/
@Controller
public class ServiceController {
	
	private static final long serialVersionUID = 1L;
	
	private static final String MODULE_PKG_PREFIX = "cn.max.services.";
	
	@RequestMapping("s/{module}/{method}")
	public ModelAndView execute(HttpServletRequest request, HttpServletResponse response, @PathVariable("module") String service, @PathVariable("method") String method) {
		
		ModuleContext mc = new ModuleContext(request, response);
		try {
			
			ADService svs = (ADService) Class.forName(MODULE_PKG_PREFIX + service).newInstance();
			
			ReflectUtils.invoke(svs, "setModuleContext", new Class[]{ModuleContext.class}, new Object[]{mc});
			
			ReflectUtils.invoke(svs, method);
			
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}